class MyBockingQueue{
    private volatile int size;
    private volatile int head;
    private volatile int tial;
    public int[] arr=null;
    public MyBockingQueue(int capity){
        arr=new int[capity];
    }

    public int take() throws InterruptedException {
        int ret;
        synchronized (this){
            while (size==0){
                this.wait();
            }
            ret=arr[tial];
            tial++;
            if(tial>=arr.length){
                tial=0;
            }
            size--;
            this.notify();
        }
        return ret;
    }


    public void put(int num) throws InterruptedException {
        synchronized (this) {
            while (size == arr.length) {
               this.wait();
            }
            arr[head]=num;
            head++;
            if(head==arr.length){
                head=0;
            }
            size++;
          this.notify();
        }
    }
}


public class Demo3 {
    public static void main(String[] args) throws InterruptedException {
        MyBockingQueue queue=new MyBockingQueue(4);
        queue.put(111);
        System.out.println("put成功");
        queue.put(111);
        System.out.println("put成功");
        queue.put(111);
        System.out.println("put成功");
        queue.take();
        System.out.println("take成功");
        queue.take();
        System.out.println("take成功");
        queue.take();
        System.out.println("take成功");
        queue.take();
        System.out.println("take成功");

    }
}
